package com.xilihui.xlh.component.ipicker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;


import com.xilihui.xlh.component.ipicker.entities.Config;

import java.util.ArrayList;
import java.util.List;

public class IPicker {

    private static IPicker instance;

    private OnSelectedListener onSelectedListener;

    private int limit = 1;               // 最多可选的图片数
    private boolean cropEnable = false;  // 是否启用裁剪
    private boolean cropAsSquare = false; // 是否正方形裁剪
    // 不是正方形裁剪的宽高
    private int width;                   // 裁剪宽度
    private int height;                  // 裁剪高度

    private IPicker() {
    }

    public static IPicker getInstance() {
        if (instance == null) {
            instance = new IPicker();
        } else {
            init();
        }
        return instance;
    }

    /**
     * 得到当前的实例
     *
     * @return
     */
    static IPicker getCurrentInstance() {
        return instance;
    }

    /**
     * 1、每次得到单例先初始化设置
     * 2、选图完成之后清除设置
     */
    private static void init() {
        instance.setLimit(1);
        instance.setCropEnable(false);
        instance.setCropDimension(0, 0);//这两个顺序不能变
        instance.setCropAsSquare();// 这两个顺序不能变
        instance.setOnSelectedListener(null);
    }

    private void setCropAsSquare() {
        this.cropAsSquare = false;
    }

    /**
     * 设置最多可选的张数
     *
     * @param limit
     */
    public void setLimit(int limit) {
        this.limit = limit;
    }

    /**
     * 设置是否启用裁剪
     *
     * @param cropEnable
     */
    public void setCropEnable(boolean cropEnable) {
        this.cropEnable = cropEnable;
    }

    /**
     * 默认正方形裁剪不需要调用此方法
     * 如需要改为长方形裁剪框，则设置对应的宽度高度即可
     *
     * @param width
     * @param height
     */
    public void setCropDimension(int width, int height) {
        this.cropAsSquare = false;
        this.width = width;
        this.height = height;
    }

    public void setOnSelectedListener(OnSelectedListener listener) {
        onSelectedListener = listener;
    }

    /**
     * Finish the IPicker and post a event to observer.
     *
     * @param path
     */
    void finish(String path) {
        List<String> paths = new ArrayList<>();
        paths.add(path);
        finish(paths);
    }

    /**
     * Finish the IPicker and post a event to observer.
     *
     * @param paths
     */
    void finish(List<String> paths) {
        if (onSelectedListener != null) {
            onSelectedListener.onSelected(paths);
        }
        onSelectedListener = null;
        instance = null;
    }

    /**
     * Open the IPicker to select photos or take pictures.
     *
     * @param context
     */
    public void open(Context context) {
        open(context, null);
    }

    /**
     * Open the IPicker to select photos or take pictures.
     *
     * @param context
     * @param selected
     */
    public void open(Context context, ArrayList<String> selected) {
        Intent intent = new Intent(context, IPickerActivity.class);
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        Config config = new Config();
        config.setLimit(limit);
        if (selected != null) {
            config.setSeleted(selected);
        }
        config.setCropEnable(cropEnable);
        config.setCropAsSquare(cropAsSquare);
        config.setCropWidth(width);
        config.setCropHeight(height);
        intent.putExtra(IPickerActivity.CONFIG, config);
        context.startActivity(intent);
    }

    public interface OnSelectedListener {
        void onSelected(List<String> paths);
    }
}
